#include "user.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <pwd.h>
#include <unistd.h>
#include <boost/filesystem.hpp>
 
void UserData::setUsername(std::string const &username)
{
    this->username = username;
}
 
void UserData::setPassword(std::string const &password)
{
    this->password = password;
}
 
std::string UserData::getUsername()
{
    return this->username;
} 
 
std::string UserData::getPassword()
{
    return this->password;
} 

void UserData::load(const std::string &filename) 
{
	std::ifstream file;
	file.open(filename.c_str());
	getline(file, this->username);
	getline(file, this->password);
	file.close();
}

void UserData::save(const std::string &filename)
{
	std::ofstream file;
	file.open(filename.c_str());
	file << this->username << "\n" << this->password;
	file.close();
}

bool UserData::exists(const std::string &filename) 
{
	return boost::filesystem::exists(filename);	
}

std::string UserData::getDefaultFilename() 
{
	int myuid;
	passwd *mypasswd;
	std::string fileName;

	myuid = getuid();
	mypasswd = getpwuid(myuid);

	fileName = mypasswd->pw_dir;
	fileName += "/medapp-user-password.txt";

	return fileName;
}

